cap cd "C:\Users\ejm5\Dropbox\PAPI conjoint\Replication"
cap cd "/Users/paulschuler/Dropbox/projects/PAPI conjoint\Replication"
set more off

 
/*Create Form A*/
#delimit;
use "RawData\papi2016_wghts.dta", clear;



svyset PSW_PSU [pweight=PSW_psweight], fpc(PSW_FPC1) strata(PSW_STRATA) singleunit(certainty) ///
 || PSW_SSU, fpc(PSW_FPC2) strata(PSW_STRATA2) || villageid, fpc(PSW_FPC3) strata(PSW_STRATA3) ///
 || _n,  poststrata(tinh)  postweight(PSW_province_population);
 
 
generate respondent_id=_n;

mvdecode idcode0 tinh tentinh huyen tenhuyen tenxa tenthon khuvuc a00* a016* a011* d305* d101d* d407
d102_tuoi1 d102_gioitinh1 d102_tinhthanh1 d102_decu1 d102_dangvien1 d102_nghenghiep1 d102_khhd1  d102 PSW* villageid respondent_id d306*, mv(111=.a\888=.b\999=.c);
 
 
keep idcode0 tinh tentinh huyen tenhuyen tenxa tenthon khuvuc a00* a016* a011* d305* d101d* d407
d102_tuoi1 d102_gioitinh1 d102_tinhthanh1 d102_decu1 d102_dangvien1 d102_nghenghiep1 d102_khhd1  d102 PSW* villageid respondent_id d306*;
generate year=2016;
generate form=2;

tab d102_tuoi1, gen(age_);
rename age_1 age_39;
rename age_2 age_59;
rename d102_tuoi1 age;

tab d102_gioitinh1, gen(male);
rename male2 male;
rename male3 female;
rename d102_gioitinh1 sex;
drop male1;

gen home=1 if d102_tinhthanh1==tinh;
replace home=0 if d102_tinhthanh1 !=tinh;
rename d102_tinhthanh home_province;

tab d102_decu1, gen(nom);
rename d102_decu1 nomination;
rename nom2 self_nominee;
rename nom3 gov_nominee;
drop nom1;


tab d102_dangvien1, gen(party);
rename d102_dangvien1 party_status;
rename party2 party;
rename party3 non_party;
drop party1;

tab d102_nghenghiep1, gen(job);
rename d102_nghenghiep1 job;
rename job2 VFF; 
rename job3 business;
drop job1;

tab d102_khhd1, gen(policy);
rename d102_khhd1 policy_priority;
rename policy2 trade;
rename policy3 poverty;
rename policy4 growth;
rename policy5 environment;
drop policy1;

rename d102 vote;
replace vote=1 if vote==1;
replace vote=0 if vote==2;
save NA_formA_2016.dta, replace;



/*Create Form B*/
#delimit;
use "RawData\papi2016_wghts.dta", clear;



svyset PSW_PSU [pweight=PSW_psweight], fpc(PSW_FPC1) strata(PSW_STRATA) singleunit(certainty) ///
 || PSW_SSU, fpc(PSW_FPC2) strata(PSW_STRATA2) || villageid, fpc(PSW_FPC3) strata(PSW_STRATA3) ///
 || _n,  poststrata(tinh)  postweight(PSW_province_population);
 
 
generate respondent_id=_n;

mvdecode idcode0 tinh tentinh huyen tenhuyen tenxa tenthon khuvuc a00* a016* a011* d305* d101d* d407
d102_tuoi1 d102_gioitinh1 d102_tinhthanh1 d102_decu1 d102_dangvien1 d102_nghenghiep1 d102_khhd1  d102 PSW* villageid respondent_id d306*, mv(111=.a\888=.b\999=.c);
 
 
keep idcode0 tinh tentinh huyen tenhuyen tenxa tenthon khuvuc a00* a016* a011* d305* d101d* d407
d102_tuoi2 d102_gioitinh2 d102_tinhthanh2 d102_decu2 d102_dangvien2 d102_nghenghiep2 d102_khhd2  d102 PSW* villageid respondent_id;
generate year=2016;
generate form=1;

tab d102_tuoi2, gen(age_);
rename age_1 age_39;
rename age_2 age_59;
rename d102_tuoi2 age;

tab d102_gioitinh2, gen(male);
rename male1 male;
rename male2 female;
rename d102_gioitinh2 sex;


gen home=1 if d102_tinhthanh2==tinh;
replace home=0 if d102_tinhthanh2 !=tinh;
rename d102_tinhthanh home_province;

tab d102_decu2, gen(nom);
rename d102_decu2 nomination;
rename nom2 self_nominee;
rename nom3 gov_nominee;
drop nom1;


tab d102_dangvien2, gen(party);
rename d102_dangvien2 party_status;
rename party2 party;
rename party3 non_party;
drop party1;


tab d102_nghenghiep2, gen(job);
rename d102_nghenghiep2 job;
rename job2 VFF; 
rename job3 business;
drop job1;

tab d102_khhd2, gen(policy);
rename d102_khhd2 policy_priority;
rename policy2 trade;
rename policy3 poverty;
rename policy4 growth;
rename policy5 environment;
drop policy1;

rename d102 vote;
replace vote=0 if vote==1;
replace vote=1 if vote==2;
replace vote=. if vote>2;
save NA_formB_2016.dta, replace;


use NA_formA_2016.dta, clear;
append using NA_formB_2016.dta;


xtset respondent_id form;
generate policy_same=1 if trade==1 & l.trade==1;
replace policy_same=1 if trade==1 & f.trade==1;
replace policy_same=1 if poverty==1 & l.poverty==1;
replace policy_same=1 if poverty==1 & f.poverty==1;
replace policy_same=1 if growth==1 & l.growth==1;
replace policy_same=1 if growth==1 & f.growth==1;
replace policy_same=1 if environment==1 & l.environment==1;
replace policy_same=1 if environment==1 & f.environment==1;
replace policy_same=0 if policy_same==.;

/*************************************ISSUE*********************/
generate issue_environment=0;
replace issue_environment=3 if regexm(d306a, "moi truong");
replace issue_environment=2 if regexm(d306b, "moi truong");
replace issue_environment=1 if regexm(d306c, "moi truong");

generate issue_poverty=0;
replace issue_poverty=3 if regexm(d306a, "ngheo");
replace issue_poverty=2 if regexm(d306b, "ngheo");
replace issue_poverty=1 if regexm(d306c, "ngheo");


generate issue_growth=0;
replace issue_growth=1 if regexm(d306a, "phat trien")|regexm(d306a, "kinh te");
replace issue_growth=2 if regexm(d306b, "phat trien")|regexm(d306b, "kinh te");
replace issue_growth=3 if regexm(d306c, "phat trien")|regexm(d306c, "kinh te");

generate issue_trade=0;
replace issue_trade=1 if regexm(d306a, "dau tu")|regexm(d306a, "thuong mai");
replace issue_trade=2 if regexm(d306b, "dau tu")|regexm(d306b, "thuong mai");
replace issue_trade=3 if regexm(d306c, "dau tu")|regexm(d306c, "thuong mai");

generate issue_eastsea = 0 ;
replace issue_eastsea=1 if d306a2==41|  d306a2==43 ;
replace issue_eastsea=2 if d306b2==41| d306b2==43 ;
replace issue_eastsea=3 if d306c2==41| d306c2==43 ;

gen party_member = 0;
replace party_member = 1 if a016_opt1==1;
gen civil_society=1;
replace civil_society= 0 if a016_opt99==1;
sum issue*;
gen respondent_age = a002;
gen respondent_male = 1;
replace respondent_male = 0 if a001==2;


#delimit;
generate match=0;
replace match=1 if environment==1 & issue_environment>0;
replace match=1 if poverty==1 & issue_poverty>0;
replace match=1 if growth==1 & issue_growth>0;
replace match=1 if trade==1 & issue_trade>0;
lab var match "Candidate and Respondent Share Policy Priority";


gen no_false = .;
replace no_false=1 if d101d5a==1;
replace no_false=1 if d101d5b==1;
replace no_false=0 if d101d5a==0;
replace no_false=0 if d101d5b==0;

save PAPI_Conjoint_2016.dta, replace;


